﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using DAL;

namespace PeisongWeb.admin
{
    public partial class iframe_productPriceList : System.Web.UI.Page
    {
        protected int id
        {
            get
            {
                int tmp;
                try { tmp = Convert.ToInt32(ViewState["id"]); }
                catch { tmp = 0; }
                return tmp;
            }
            set
            { ViewState["id"] = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["category"] != null)
                {
                    if (Library.Number.IsIntPositive(Request.QueryString["category"]))
                    {
                        id = Convert.ToInt32(Request.QueryString["category"]);
                    }
                }
                rptBind();
            }
        }
        void rptBind()
        {
            StringBuilder where = new StringBuilder();
        
            where.Append(" isSale=1 and categoryId=" + id);
            //where.Append(" and isAutoPrice=0 "); //获得不能自动加价的商品
      
            DataSet ds = logic.product.listWithNewPrice(where.ToString(), "categoryId", 8000, 1);

            rpt_list.DataSource = ds.Tables[1];
            rpt_list.DataBind();
        }


        //选择供应商
        protected void rpt_list_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            Array lists = e.CommandArgument.ToString().Split(',');
            int productId = Convert.ToInt32(lists.GetValue(0));
            string nature = lists.GetValue(1).ToString();
            if (e.CommandName == "updateSeller")
            {
                hdUpdateProduct.Value = productId.ToString();
                bindWinUpdateSeller(productId,nature);
            }
            else if (e.CommandName == "updatePrice")
            {
                updatePrice(productId,nature);
            }
        }

        //非自动加价商品，管理员修改价格
        void updatePrice(int productId, string nature)
        {
            decimal price=0;
            foreach(RepeaterItem row in rpt_list.Items)
            {
                if (row.ItemType == ListItemType.AlternatingItem || row.ItemType == ListItemType.Item)
                {
                    HiddenField hd = row.FindControl("hd_ItemProductId") as HiddenField;
                    HiddenField hdNature = row.FindControl("hd_busNature") as HiddenField;

                    if (hd.Value == productId.ToString() && hdNature.Value == nature)
                    {
                        TextBox txt_price = row.FindControl("txt_Price") as TextBox;
                        price = Convert.ToDecimal(txt_price.Text);
                        break;
                    }
                }
                
            }


            PriceEntity pr = new PriceEntity();
            pr.Price = price;
            pr.ProductId = productId;
            pr.BusinessNature = nature;
            pr.AdminId = logic.sysAdmin.AdminID;
            logic.price.updateByAdmin(pr);

            logic.buyerPriceHistory.insertByUpdateProduct(productId); //增加采购商历史价格
            rptBind();      
        
        }

        

        void bindWinUpdateSeller(int productId,string nature)
        {
            DataSet ds = logic.priceSeller.getCurrenSellerPriceByProduct(productId, nature);

            ddl_updateSeller.DataSource = ds.Tables[0];
            ddl_updateSeller.DataBind();

            lbl_Product.Text = ds.Tables[1].Rows[0][0].ToString();

            win_Seller.Show();
            //rptBind();
           
        }
        protected void btn_updateSeller_Click(object sender, EventArgs e)
        {
            int psId = Convert.ToInt32(ddl_updateSeller.SelectedValue);
            int adminId = logic.sysAdmin.AdminID;
            //修改供应商
            logic.price.changeSellerInfor(psId,adminId);
            logic.buyerPriceHistory.insertByUpdateProduct(Convert.ToInt32(hdUpdateProduct.Value)); //增加采购商历史价格  
            win_Seller.Hide();            
            Library.Script.ClientMsg("操作成功！");
                     
            rptBind();
        }

    }
}
